$(function(){
	ajax({
			'url':'/admin/Singlerate/region',
			'type':'post',
			'data':{},
			'dataType':'json',
			'success':function(response) {
				if(response.success=='OK'){
					var data = response.data;
					var data_sheng = [];
					var sheng = '#sheng';
					var shi = '#shi';
					for (var i = 0; i < data.length; i++) {
						$(sheng).append($('<option value="'+data[i].id+'">'+data[i].name+'</option>'));
						data_sheng[data[i].id] = data[i];
					}
					$(sheng).on('change',function(){
						var thisid = $(this).val();
						if(thisid == "" || thisid == null || thisid == undefined){
							$(shi).find('option').remove();
							$(shi).append($('<option value="0" disabled="disabled" selected="selected">请选择城市</option>'));
							return false;

						}
						var shi_ = data_sheng[thisid].shi;
						var provinceid_shi = $(shi).attr('provinceid');
						if(provinceid_shi==thisid){
							return false;
						}
						$(shi).attr('provinceid',thisid);
						$(shi).empty();
						$(shi).append($('<option value="0" disabled="disabled" selected="selected">请选择省份</option>'));
						for (var i = 0; i < shi_.length; i++) {
							$(shi).append($('<option value="'+shi_[i].id+'">'+shi_[i].name+'</option>'));
						}
					});
				}
			}
	})
});



/**
 * 城市过单,单个搜索
 * @param  {[type]} ){	var cutomecity    [description]
 * @return {[type]}         [description]
 */
$(".cucitytype").click(function(){
	ajaxcityadopt("","cucitytype");
});
/**
 * 城市过单多个搜索
 */
$(".cutomecity").click(function(){
	ajaxcityadopt("","cutomecity");
});
/**
 * 城市通过订单
 * @param  {[type]} data [description]
 * @return {[type]}      [description]
 */
function ajaxcityadopt(page,datatype){

	if(datatype == "cucitytype"){
		var cutomecity = $("input[name='cutomecity']").val().trim();
		if(!cutomecity){
			sweetAlert('','城市名称不能为空','error');
	        return false;
		}
		data = {cityname:cutomecity};
	}else if(datatype == "cutomecity"){
		var start = $("#start").val().trim();//开始月
		var end   = $("#end").val().trim();//结束月
		var sheng = $("#sheng").val();//省
		var shi   = $("#shi").val();//市
		var startrate   = $("#startrate").val().trim();//开始百分比
		var endrate     = $("#endrate").val().trim();//结束百分比
		var signrate    = $("#signrate").val().trim();//开始标记百分比
		var signendrate = $("#signendrate").val().trim();//结束标记百分比
		var signeradio  = $("input[name='0']:checked").val();
        if(signeradio == "1"){
            if(!isIntNum(start) || !isIntNum(end) || end == "" || start == ""){
                sweetAlert('', '结束月不能为空或请输入数字', 'error');
                return false;
            }
            if(parseInt(start) > parseInt(end)){
                sweetAlert('','起始月不能大于结束月','error');
                return false; 
            }
            if(start == 0 || end == 0){
                sweetAlert('','起始月不能输入零','error');
                return false; 
            }
            if(!isIntNum(start) || !isIntNum(end)){
                sweetAlert('','请输入数字','error');
                return false;
            }
        }else{
            var selectvale = $(".select_time").val();
            if(selectvale != 0){
                start = selectvale;
                end   = selectvale;
            }else if(selectvale == 0){
                start = 1;
                end   = 12;
            }
        }
		if(startrate == "" || endrate == ""){
			startrate = 0;
			endrate   = 100;
		}else if(parseInt(startrate) > parseInt(endrate)){
            sweetAlert('','起始值不能大于结束值','error');
            return false;
        }
        if(!isIntNum(startrate) || !isIntNum(endrate)){
            sweetAlert('','请输入数字','error');
            return false;
        }
		data ={start:start,end:end,
			   typelv:"toorate",
			   provinceid:sheng,
			   cityid:shi,
			   startrate:startrate,
			   endrate:endrate,
			   signrate:signrate,
			   signendrate:signendrate,
			  }
	}
	if(!page){
		page=1;
	}
	ajax({
		'url':'/admin/Singlerate/Slerate',
		'type':'post',
		'data':data,
		'dataType':'json',
		'success':function(response) {
            if(response.resdata && response.resdata.length != 0){
                setItem('orders', response.resdata);
                var totalPage = Math.ceil(response.resdata.length / 15);
                showPage(1, totalPage, addInfo);recovery();
                addInfo(1, totalPage);
            }else{
                addNotify();
            }
			
			// addInfo(response.page,response.resdata);
		}
	},true)
}

/**
 * 过单率
 * @param {[type]} data [description]
 */
function addInfo(page,totalPage) {
        // if (data.length <= 0) {
        //     $('.table_hover tr').remove();
        //     return;
        // }
        var data = pageData(page,totalPage)
        if (data.length <= 0) {
            $('.table_hover tr').remove();
            return;
        }
        showPage(page,totalPage,addInfo);
        $('.table_hover tr').remove();
        $('.tr_item_title').children().remove();
        var children = $('.tr_checkbox').children();
        for (var i = 0; i < children.length; i++) {
            children[i].remove();
        }
        var th = '';
        var tr = '';
        //var months = ['当月', '上月', '第3月', '第4月', '第5月', '第6月', '第7月', '第8月', '第9月', '第10月', '第11月', '第12月'];
        tr += "<th>序号</th> <th>城市名称</th> <th>所属省份</th> <th>GW在职总数</th> <th>城市渠道激活数</th><th>CS姓名</th> ";
        tr += "<th>CS岗位号</th> <th>SZ姓名</th>  <th>SZ岗位号</th> <th>总过单数</th> <th>总过单比</th> <th>总过单率</th>";
        th += "<th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th>";
        th += "<th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th>";
    
        var trStatus = true;
        for (var i = 0; i < data.length; i++) {
        	cityname = "";
        	citygwhn = "";
            var str = '';
            str += "<td>" + (i + 1) + "</td>";
            str += "<td>" + data[i].cityname+ "</td>";
            str += "<td>" + data[i].provanem + "</td>";
            str += "<td>" + data[i].inc + "</td>";
            str += "<td>" + data[i].wayinc + "</td>";
            str += "<td>" + data[i].name + "</td>";
            str += "<td>" + data[i].usernum + "</td>";
            str += "<td>" + data[i].szname + "</td>";
            str += "<td>" + data[i].szusernum + "</td>";
            var orders = data[i].guodanarr;
            for (var j = 0; j < orders.length; j++) {
            	if(orders[j].tooorder || orders[j].tooorderccount || orders[j].tooordercount){
            		str += "<td>" + orders[j].tooorder + "</td>";
                    str += "<td>" + orders[j].tooordercount + "</td>";
                    str += "<td>" + orders[j].tooorderccount + "%</td>";
            	}
            }
            for (var j = 0; j < orders.length; j++) {
            	if(orders[j].ordercoun || orders[j].ordermon || orders[j].orderrate){
            		str += "<td>" + orders[j].ordercoun + "</td>";
                    str += "<td>" + orders[j].ordermon + "</td>";
                    str += "<td>" + orders[j].orderrate + "%</td>";
            	}
                    
                
            }
            if(trStatus){
            	for (var j = data[i].start; j <= data[i].end; j++) {
            		
            		if(j == 1){
            			months = "当月";
            		}else if(j == 2){
            			months = "上月";
            		}else{
            			months = "第"+j+"月";	
            		}
                    tr += "<th>" + months + "过单数</th>";
                    tr += "<th>" + months + "过单比</th>";
                    tr += "<th>" + months + "过单率</th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
            	}
                th+='<th calss="btn_rate selected" id="btn_filter">'+
                      '<button type="button" class="btn btn-warning btn_filter" >过滤</button>'+
                      '</th>'+
                      '<th calss="btn_rate selected" id="btn_recovery">'+
                      '<button type="button" class="btn btn_recovery" >重置</button>'+
                      '</th>';
            }
            var trStatus = false;
            var trcolor = "";
            if(data[i].typesign == "red"){
            	trcolor = "color:red";
            }
            str += "<td class='action_list'><a href='./cityrisk.html?cutomecity="+data[i].id+"'><span>查看详情</span></a></td>";
            str += "<td></td>";
            var str2 = "<tr style="+trcolor+">" + str + "</tr>";
            $('.table_hover').append(str2);
        }
        tr += "<th class='action'>操作</th>";
        tr += "<th class='action'></th>";
        $('.tr_item_title').append(tr);
        $('.thead_item .tr_checkbox').append(th);
        if (!filterTag) {
            tableHide();
        }else{
            recovery();
        }
       
}

$('.radio').click(function () {
        var value = $(this).val();
        switch(value) {
            case "0":
                $('#start').attr("disabled", "disabled");
                $('#end').attr("disabled", "disabled");
                $("#start").val("");
                $("#end").val("");
                $('.select_time').removeAttr("disabled");
                break;
            case "1":
                $('.select_time').attr("disabled", "disabled");
                $('#start').removeAttr("disabled");
                $('#end').removeAttr("disabled");
                break;
        }
    });
$('.radios').click(function () {
        var value = $(this).val();
        switch(value) {
            case "2":
                $('.start').attr("disabled", "disabled");
                $('.end').attr("disabled", "disabled");
                $("#start").val("");
                $("#end").val("");
                $('.select_timeend').removeAttr("disabled");
                break;
            case "3":
                $('.select_timeend').attr("disabled", "disabled");
                $('.start').removeAttr("disabled");
                $('.end').removeAttr("disabled");
                break;
        }
    });

$('body').on('click', '.num', function()
{
    var value = parseInt($(this).attr('value'));
    var number = $('.end').attr('value');
    if (value == 0) return;
    if (value > number) return;
    var end = 10 * value;
    var start = end - 10 + 1;
    var result = pageData(start, end);
    getkehuinfo(result);
    $('.prev').attr('value', value - 1);
    $('.next').attr('value', value + 1);
    $('.num').removeClass('a_active');
    var childrens = $('.black2').children();
    var num = $(this).attr('number');
    childrens.eq(num).addClass('a_active');
    $('.prev').attr('number', parseInt(num) - 1);
    $('.next').attr('number', parseInt(num) + 1);
});



/**
 * 过单导出数据
 */
$(".ExportExcelUpper").click(function(){
	var list = $(this).val();
	if(!list){
		sweetAlert('','导出程序错误','error');
	    return false;
	}
	ajax({
		'url':'/admin/Singlerate/ExportExcelUpper',
		'type':'post',
		'data':{status:list,arrarray:filterIndexs},
		'dataType':'json',
		'success':function(response) {
			if(response.success == "OK"){
                location.href = response.path;

			}
		}
	},true)
})